package com.zunipe.minix;

import com.zunipe.ZunipeTools;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.font.TextRenderer;
import net.minecraft.client.render.VertexConsumerProvider;
import net.minecraft.client.render.entity.EntityRenderDispatcher;
import net.minecraft.client.render.entity.EntityRenderer;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.entity.Entity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.decoration.ArmorStandEntity;
import net.minecraft.entity.effect.StatusEffect;
import net.minecraft.entity.effect.StatusEffectInstance;
import net.minecraft.entity.effect.StatusEffects;
import net.minecraft.entity.mob.ShulkerEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.text.LiteralText;
import net.minecraft.text.Text;
import net.minecraft.util.math.Matrix4f;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(EntityRenderer.class)
public abstract class EntityHealthInfoShowMixin<T extends Entity>
{
    @Shadow
    public abstract TextRenderer getTextRenderer();

    @Shadow
    public EntityRenderDispatcher dispatcher;

    @Inject(method = "render(Lnet/minecraft/entity/Entity;FFLnet/minecraft/client/util/math/MatrixStack;Lnet/minecraft/client/render/VertexConsumerProvider;I)V", at = @At("HEAD"), cancellable = true)
    public void render(T entity, float yaw, float tickDelta, MatrixStack matrices, VertexConsumerProvider vertexConsumers, int light, CallbackInfo info)
    {
        double d = this.dispatcher.getSquaredDistanceToCamera(entity);
        if (d > 4096)
            return;
        if (MinecraftClient.getInstance().player == null || MinecraftClient.getInstance().player == entity)
            return;
        if (entity instanceof LivingEntity && !(entity instanceof ArmorStandEntity))
        {
            if (entity instanceof ShulkerEntity shulker)
            {
                NbtCompound nbt = new NbtCompound();
                shulker.writeNbt(nbt);
                if (nbt.contains("NoGravity") && nbt.getDouble("NoGravity") == 1)
                    return;
            }

            String name = entity.getType().getName().getString();
            float f = entity.getHeight() + 0.5f;
            float absorption = 0;

            if (entity instanceof PlayerEntity)
            {
                absorption = ((PlayerEntity) entity).getAbsorptionAmount();
                f += 0.5f;
                name = entity.getName().asString();
            }
            String s = String.format("§6%s §a§l%.1f§r§l | §a§l%.1f", name, ((LivingEntity) entity).getHealth(), ((LivingEntity) entity).getMaxHealth());
            if (absorption != 0)
                s = String.format("§6%s §a§l%.1f§r§l | §a§l%.1f §b§l%.1f", name, ((LivingEntity) entity).getHealth() + absorption, ((LivingEntity) entity).getMaxHealth() + absorption, absorption);
            Text text = new LiteralText(s);

            matrices.push();
            matrices.translate(0.0, f, 0.0);
            matrices.multiply(this.dispatcher.getRotation());
            matrices.scale(-0.025f, -0.025f, 0.025f);
            Matrix4f matrix4f = matrices.peek().getModel();
            float g = MinecraftClient.getInstance().options.getTextBackgroundOpacity(0.25f);
            int j = (int) (g * 255.0f) << 24;
            TextRenderer textRenderer = this.getTextRenderer();
            float h = -textRenderer.getWidth(text) / 2.0f;
            textRenderer.draw(text, h, 0, 0x20FFFFFF, false, matrix4f, vertexConsumers, true, j, 0xF000F0);
            textRenderer.draw(text, h, 0, 16711680, false, matrix4f, vertexConsumers, false, 0, 0xF000F0);
            matrices.pop();
        }
    }
}
